﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using deckAnalyser.Domain;

namespace deckAnalyser.Controls
{
  public partial class ManaCompositionControl : UserControl
  {
    private Deck deck = null;
    public Deck Deck
    {
      get { return deck; }
      set
      {
        if (deck != null)
          deck.OnUpdate -= OnDeckUpdate;

        deck = value;

        if (deck != null)
          deck.OnUpdate += OnDeckUpdate;

        UpdateGraph();
      }
    }

    void OnDeckUpdate(DomainObject d)
    {
      UpdateGraph();
    }
    

    public ManaCompositionControl()
    {
      InitializeComponent();
    }

    public void UpdateGraph()
    {
      GraphPane zgcPane = zgc.GraphPane;
      zgcPane.CurveList.Clear();

      if (deck == null)
      {
        zgc.AxisChange();
        zgc.Refresh();
        return;
      }

      zgcPane.Title.Text = "Mana Composition";
      zgcPane.XAxis.Title.Text = "Types";
      zgcPane.YAxis.Title.Text = "Count";

      Dictionary<String,int[]> composition = deck.GetManaComposition(true);

      string[] labels = composition.Keys.ToArray();
      
      double[] white = (from key in composition.Keys.Cast<string>() select (double)composition[key][0]).ToArray();
      double[] blue = (from key in composition.Keys.Cast<string>() select (double)composition[key][1]).ToArray(); ;
      double[] black = (from key in composition.Keys.Cast<string>() select (double)composition[key][2]).ToArray(); ;
      double[] red = (from key in composition.Keys.Cast<string>() select (double)composition[key][3]).ToArray(); ;
      double[] green = (from key in composition.Keys.Cast<string>() select (double)composition[key][4]).ToArray(); ;
      double[] artifact = (from key in composition.Keys.Cast<string>() select (double)composition[key][5]).ToArray(); ;



      BarItem myCurve = zgcPane.AddBar(null, null, white, Color.White);
      myCurve.Bar.Fill = new Fill(Color.Wheat, Color.White,Color.Wheat);

      myCurve = zgcPane.AddBar(null, null, blue, Color.Blue);
      myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);

      myCurve = zgcPane.AddBar(null, null, black, Color.Black);
      myCurve.Bar.Fill = new Fill(Color.Black, Color.White, Color.Black);

      myCurve = zgcPane.AddBar(null, null, red, Color.Red);
      myCurve.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);

      myCurve = zgcPane.AddBar(null, null, green, Color.Green);
      myCurve.Bar.Fill = new Fill(Color.Green, Color.White, Color.Green);

      myCurve = zgcPane.AddBar(null, null, artifact, Color.Gray);
      myCurve.Bar.Fill = new Fill(Color.Gray, Color.White, Color.Gray);


      zgcPane.XAxis.MajorTic.IsBetweenLabels = true;

      zgcPane.XAxis.Scale.TextLabels = labels;
      zgcPane.XAxis.Type = AxisType.Text;

      zgcPane.BarSettings.Type = BarType.Stack;

      zgcPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 200), -45F);
      zgcPane.XAxis.MajorGrid.IsVisible = true;
      zgcPane.XAxis.MajorGrid.Color = Color.LightGray;
      zgcPane.YAxis.MajorGrid.IsVisible = true;
      zgcPane.YAxis.MajorGrid.Color = Color.LightGray;

      zgc.AxisChange();
      zgc.Refresh();
    }
  }
}
